package com.wn.study.repository

import androidx.lifecycle.LiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.wn.service.db.UserDao
import com.wn.service.db.UserInfo
import com.wn.study.repository.api.StudyService
import com.wn.study.repository.data.Daily2
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2023/6/29 16:41
 * @Description: TODO
 */
const val PAGE_SIZE = 8

@ViewModelScoped
class StudyRepository @Inject constructor(
  /*private val pagingSource: StudyPagingSource,*/
  private val studyService: StudyService,
  private val userDao: UserDao,
) :
  IStudyRepository {
  /**
   * 获取 liveData-userinfo的 dao 操作
   * @return LiveData<UserInfo>
   */
  override fun getLiveUserInfo(): LiveData<UserInfo> = userDao.queryLiveUser()

  override fun getDaily(): Flow<PagingData<Daily2.Item>> = Pager(
    PagingConfig(
      pageSize = PAGE_SIZE,
//      initialLoadSize = PAGE_SIZE * 2 // 初始加载定义请求的加载大小，通常大于 pageSize  默认 3
    )
  ) { StudyPagingSource(studyService) }// 下拉刷新-- Repository中 每次都需要 不同的 PagingSource 实例  那里无需 hilt
    .flow
    .flowOn(Dispatchers.IO)
}